package com.formiaomiao.demain.controller;

import com.formiaomiao.demain.model.DemainBean;
import com.formiaomiao.demain.model.ImgBean;
import com.formiaomiao.demain.service.DemainService;
import com.formiaomiao.demain.utils.QiniuUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping(value = "/demain")
public class DemainController {

    @Autowired
    DemainService demainService;

    QiniuUtil qiniuUtil = new QiniuUtil();

    /**
     * 查询动态列表
     *
     * @param id
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectDemain")
    private Object SelectDemain(@RequestParam(name = "id", required = false) Integer id,
                                @RequestParam(name = "fromid", required = false) Integer fromid,
                                @RequestParam(name = "pageNum", required = false) Integer pageNum,
                                @RequestParam(name = "pageSize", required = false) Integer pageSize) {
        return demainService.SelectDemain(id, fromid, pageNum, pageSize);
    }

    @ResponseBody
    @RequestMapping(value = "/insertDemain")
    private Object InsertDemain(@RequestParam(name = "demainimg", required = false) MultipartFile[] multipartFile,
                                @RequestParam(name = "id", required = false) Integer id,
                                @RequestParam(name = "content", required = false) String content,
                                @RequestParam(name = "contentDate", required = false) String contentDate,
                                @RequestParam(name = "address", required = false) String address) {
        DemainBean bean = new DemainBean();
        String uid = UUID.randomUUID().toString();
        bean.setContent(content);
        bean.setAddress(address);
        bean.setContentDate(contentDate);
        bean.setDataid(uid);
        bean.setIduser(id);
        List<ImgBean> urllist = new ArrayList<>();
        ImgBean imgBean = null;
        FileInputStream inputStream = null;
        if (multipartFile != null) {
            for (int i = 0; i < multipartFile.length; i++) {
                imgBean = new ImgBean();
                try {
                    inputStream = (FileInputStream) multipartFile[i].getInputStream();
                    imgBean.setIddata(uid);
                    String usl = qiniuUtil.uploadImg(inputStream, "heademainimg" + i + uid);
                    imgBean.setUrl(usl.split(",")[0]);
                    imgBean.setImgkey(usl.split(",")[1]);
                    urllist.add(imgBean);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            bean.setImgBean(urllist);
        }
        return demainService.InsertDemain(bean);
    }

    /**
     * 删除动态
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteDemain")
    private Object DeleteDemain(@RequestParam(name = "id", required = false) String id) {
        DemainBean bean = new DemainBean();
        bean.setDataid(id);
        return demainService.DeleteDemain(bean);
    }

    /**
     * 获取所有动态列表
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectAllDemain")
    private Object selectAllDemain(@RequestParam(name = "id", required = false) Integer id,
                                   @RequestParam(name = "pageNum", required = false) Integer pageNum,
                                   @RequestParam(name = "pageSize", required = false) Integer pageSize) {
        return demainService.SelectAllDemain(id, pageNum, pageSize);
    }
}
